package com.idanchuang.component.consumer;

import com.idanchuang.component.base.actuator.DcInfoEndpoint;
import com.idanchuang.component.consumer.config.RpcConfig;
import com.idanchuang.sun.client.common.tag.TagCollector;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.env.Environment;

import java.util.Collections;

@Configuration
@EnableDiscoveryClient
@Import(ConsumerRestTemplateAutoConfiguration.class)
public class ConsumerAutoConfiguration {

    @Bean("idanchuangConsumerRpcConfig")
    @ConfigurationProperties(prefix = "idanchuang.component.consumer.rpc")
    public RpcConfig rpcConfig() {
        return new RpcConfig();
    }

    @Bean
    @ConditionalOnMissingBean(name = "acgFeignRequestInterceptor")
    public FeignRequestInterceptor feignRequestInterceptor(RpcConfig rpcConfig) {
        return new FeignRequestInterceptor(rpcConfig);
    }

    @Bean
    @ConditionalOnMissingBean(name = "acgFeignPressureInterceptor")
    public FeignPressureInterceptor feignPressureInterceptor() {
        return new FeignPressureInterceptor();
    }

    @Bean
    @ConditionalOnMissingBean(name = "dcComponentBaseTagCollector")
    public TagCollector dcComponentBaseTagCollector() {
        return () -> Collections.singleton(DcInfoEndpoint.getComponentVersion());
    }

}
